<?php
/*
 * #文件分享-文件模型
 *      模型类---王昊天写
 */
class File extends CI_Model {

    function __construct()
    {
        parent::__construct();
    }
    function add($uid,$file_path,$swf_path,$catagory,$subject,$year,$title,$tag,$description,$filename)
    {
        $data = array(
                'uid' => $uid,
                'file_path' => $file_path,
                'swf_path' => $swf_path,
                'category' => $catagory,
                'subject' => $subject,
                'year' => $year,
                'title' => $title,
                'tag' => $tag,
                'description' => $description,
                'file_name' => $filename
        );
        $this->db->insert('dy_upload',$data);
        $query = $this->db->query("SELECT fid FROM dy_upload WHERE uid = '$uid' AND file_path = '$file_path' AND file_name = '$filename'");
        $data = $query->result_array();
        $data_needed = end($data);
        
        return $data_needed['fid'];     
    }
    function get($fid)
    {
        $query = $this->db->query("SELECT * FROM dy_upload WHERE fid = '$fid'");
        $answersheet = $query->result_array();
        return $answersheet[0];
    }
    function get_all($uid)
    {
        $query = $this->db->query("SELECT file_name FROM dy_upload WHERE uid = '$uid'");
        $answersheet = $query->result_array();
        return $answersheet;
    }
    function get_all_by_category($category){
    	$query = $this->db->query("SELECT file_name FROM dy_upload WHERE category = '$category'");
    	$answersheet = $query->result_array();
    	return $answersheet;
    }
	//by sxjscience
	function get_upload_times($uid){
		if($uid==0){return 0;}
		$this->db->select("fid");
		$this->db->where('uid',$uid);
		$result=$this->db->get("dy_upload");
		return $result ? $result->num_rows() :0;
	}
	//end 
    function add_rank($fid,$rank)
    {
        $this->load->model('user/User_model');
        if($rank > 5 || $rank < 1)
                return FALSE;
        $query = $this->db->query("SELECT rank FROM dy_upload WHERE fid = '$fid'");
        $rank_before = $query->result_array();
        $query2 = $this->db->query("SELECT rank_times FROM dy_upload WHERE fid = '$fid'");
        $rank_times = $query2->result_array();
        $uid = $this->User_model->get_uid();
        $query3 = $this->db->query("SELECT rank FROM dy_rank_record WHERE uid = $uid AND fid = $fid");
        $flag = $query3->result_array();
        //var_dump($flag);
        
        if($flag)
        {
                if($rank_times[0]['rank_times']==1){
                        $data = array(
                        'rank' => $rank
                        );
                        $this->db->where('fid',$fid);
                        $this->db->update('dy_upload',$data);
                        $this->db->where('uid',$uid);
                        $this->db->where('fid',$fid);
                        $this->db->update('dy_rank_record',$data);
                        return TRUE;
                }
                //echo $rank_before[0]['rank'];
                $old_rank = $this->db->query("SELECT rank FROM dy_rank_record WHERE uid = $uid AND fid = $fid");
                $old_rank = $old_rank->result_array();
                //var_dump($old_rank);
                $new_rank = ($rank_before[0]['rank'] - $old_rank[0]['rank']) / ($rank_times[0]['rank_times'] - 1);
                //echo $new_rank;
                $new_rank = ($new_rank * ($rank_times[0]['rank_times'] - 1) + $rank);                  
                //echo $new_rank . "<br>";
                $data = array(
                'rank' => $new_rank
                );
                $this->db->where('fid',$fid);
                $this->db->update('dy_upload',$data);
                $data = array(
                'rank' => $rank
                );
                $this->db->where('uid',$uid);
                $this->db->update('dy_rank_record',$data);
                return TRUE;
        }
        
        $new_rank = ($rank_before[0]['rank'] + $rank);
        $data = array(
                'rank' => $new_rank,
                'rank_times' => $rank_times[0]['rank_times']+1
        );
        $this->db->where('fid',$fid);
        $this->db->update('dy_upload',$data);
        $data2 = array(
                'fid' => $fid,
                'uid' => $uid,
                'rank' => $rank
        );
        $this->db->insert('dy_rank_record',$data2);
        return TRUE;
    }
    function final_rank($flag,$fid){
    	if($flag){
			$final_rank = $this->db->query("SELECT rank,rank_times FROM dy_upload WHERE fid = '$fid'");
			$final_rank = $final_rank->result_array();
			$final_rank = $final_rank[0]['rank'] / $final_rank[0]['rank_times'];
			printf("%.1f",$final_rank);
		}else
			echo "add_rank failed!";
    }
    function add_download_times($fid){
    	$download_times = $this->db->query("SELECT download_times FROM dy_upload WHERE fid = '$fid'");
    	$download_times = $download_times->result_array();
    	$download_times = $download_times[0]['download_times'];
    	$data = array(
    		'download_times' => $download_times + 1
    	);
    	$this->db->where('fid',$fid);
    	$this->db->update('dy_upload',$data);	
    }
    function delete($fid){
        $this->db->delete('dy_upload',array('fid'=>$fid));
    }
    /*
     *按分类返回列表
     *  慎力博
     */
        function list_by_category($category,$start=0,$pages=30)
        {
                $this->db->select("*");
		$this->db->order_by("fid","desc");
                $this->db->where("category",$category);
                $query = $this->db->get("dy_upload",$pages,$start);
                return $query->result_array();
        }
        function get_count_by_category($category)
        {
                $query = $this->db->query('SELECT * FROM dy_upload');
                return $query->num_rows();
        }
		
		//排行前五名
		function get_rank($limit=5)
		{
			$this->db->order_by('rank','desc');
			$query=$this->db->get('dy_upload',$limit,0);
			return $query->result();
		}
		//最新分享
		function get_newest($limit=5)
		{
			$this->db->order_by('fid','desc');
			$query=$this->db->get('dy_upload',$limit,0);
			return $query->result();
		}
}


/*
 * file name: file.php
 */
